// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
#pragma once
#include "pch.h"
#include "MessageDialog.h"

using namespace concurrency;
using namespace Platform;
using namespace Windows::UI::Popups;

// Displays a MessageDialog control that contains the provided content and title, and an OK button.
task<void> Weathr::MessageDialog::ShowAcceptDialogAsync(String^ content, String^ title)
{
    // Create the message dialog and set its content.
    auto messageDialog = ref new Windows::UI::Popups::MessageDialog(content, title);

    // Add the command to the dialog.
    auto okCommand = ref new UICommand("OK");
    messageDialog->Commands->Append(okCommand);

    // Set the command that will be invoked by default and when escape is pressed.
    messageDialog->DefaultCommandIndex = 0;
    messageDialog->CancelCommandIndex = 0;

    // Show the message dialog.
    return create_task(messageDialog->ShowAsync()).then([](IUICommand^ command)
    {
        // We use a continuation here so we don't have to return task<IUICommand> from the parent function.
        (void) command;
    });
}

// Displays a MessageDialog control that contains the provided content and title, and OK and Cancel buttons.
task<Weathr::DialogResult> Weathr::MessageDialog::ShowConfirmDialogAsync(String^ content, String^ title)
{
    // Create the message dialog and set its content.
    auto messageDialog = ref new Windows::UI::Popups::MessageDialog(content, title);

    // Add commands to the dialog.
    auto okCommand = ref new UICommand("OK");
    auto cancelCommand = ref new UICommand("Cancel");
    messageDialog->Commands->Append(okCommand);
    messageDialog->Commands->Append(cancelCommand);

    // Set the command that will be invoked by default.
    messageDialog->DefaultCommandIndex = 0;

    // Set the command to be invoked when escape is pressed.
    messageDialog->CancelCommandIndex = 1;

    // Show the message dialog.
    return create_task(messageDialog->ShowAsync()).then([okCommand, cancelCommand](IUICommand^ result)
    {
        assert(result == okCommand || result == cancelCommand);
        return result == okCommand ? Weathr::DialogResult::OK : Weathr::DialogResult::Cancel;
    });
}